package com.qianbao.qianbaobusiness.ui.mine.personalinfo.email;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.androidbase.utils.RegexUtils;
import com.androidbase.utils.ToastUtils;
import com.baseandroid.config.Global;
import com.baseandroid.mvp.BaseMvpActivity;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.UserInfo;
import com.qianbao.qianbaobusiness.ui.widget.EditTextWithDel;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author: Routee
 * @date 2018/6/20
 * @mail wangc4@qianbaocard.com
 * ------------1.本类由Routee开发,阅读、修改时请勿随意修改代码排版格式后提交到git。
 * ------------2.阅读本类时，发现不合理请及时指正.
 * ------------3.如需在本类内部进行修改,请先联系Routee,若未经同意修改此类后造成损失本人概不负责。
 */
public class EmailSettingActivity extends BaseMvpActivity<EmailSettingView, EmailSettingPresenter> implements EmailSettingView {

    @BindView(R.id.tv_title)
    TextView        mTvTitle;
    @BindView(R.id.tv_right_click)
    TextView        mTvRightClick;
    @BindView(R.id.et_email)
    EditTextWithDel mEtEmail;
    private UserInfo.User mUser;
    private String        mEmail;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_email_setting;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        mTvTitle.setText("编辑邮箱");
        mTvRightClick.setVisibility(View.VISIBLE);
        mTvRightClick.setText("保存");
        mTvRightClick.setTextColor(ContextCompat.getColor(this, R.color.text_red));
        mUser = Global.getUserInfoBean().getUser();
        mEtEmail.setText(mUser.getEmail());
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {

    }

    @OnClick({R.id.iv_back_toolbar, R.id.tv_right_click})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back_toolbar:
                finish();
                break;
            case R.id.tv_right_click:
                updateEmail();
                break;
            default:
                break;
        }
    }

    private void updateEmail() {
        mEmail = mEtEmail.getText().toString();
        if (!RegexUtils.isEmail(mEmail)) {
            ToastUtils.showLongToast("Email格式不合法，请重新输入");
            return;
        }
        getPresenter().setUserEmail(mEmail, mUser.getUserNo());
    }

    @Override
    public void setUserEmailSuccess() {
        mUser.setEmail(mEmail);
        UserInfo userInfoBean = Global.getUserInfoBean();
        userInfoBean.setUser(mUser);
        Global.setUserInfoBean(userInfoBean);
        finish();
    }
}
